package com.dnapipeline.modelManage.controller;

import com.dnapipeline.modelManage.entity.ProfileModel;
import com.dnapipeline.modelManage.repository.ProfileRepository;
import com.dnapipeline.modelManage.service.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/profiles")
public class ProfileController {

    @Autowired
    private ProfileRepository profileRepository;

    @Autowired
    private ProfileService profileService;

    // 获取所有ProfileModel
    @GetMapping
    public ResponseEntity<List<ProfileModel>> getAllProfiles() {
        List<ProfileModel> profiles = profileRepository.findAll();
        return ResponseEntity.ok(profiles);
    }

    // 根据deviceKey获取对应的ProfileModel列表
    @GetMapping("/device/{deviceKey}")
    public ResponseEntity<List<ProfileModel>> getProfilesByDeviceKey(@PathVariable String deviceKey) {
        List<ProfileModel> profiles = profileRepository.findByDeviceKey(deviceKey);
        return ResponseEntity.ok(profiles);
    }

    // 根据profileModelKey获取characteristicNodeId和value
    @GetMapping("/{profileModelKey}")
    public ResponseEntity<ProfileModel> getProfile(@PathVariable String profileModelKey) {
        System.out.println("查询profileModelKey: " + profileModelKey);
        ProfileModel profile = profileService.getProfileDetails(profileModelKey);
        if (profile != null) {
            return ResponseEntity.ok(profile);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    // 更新value
    @PostMapping("/{profileModelKey}/value/{value}")
    public ResponseEntity<ProfileModel> updateProfileValue(@PathVariable String profileModelKey, @PathVariable String value) {
        ProfileModel updatedProfile = profileService.updateProfileValue(profileModelKey, value);
        if (updatedProfile != null) {
            return ResponseEntity.ok(updatedProfile);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }
} 